package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.seckill.TbSeckillGoods;
import com.dongdongshop.pojo.seckill.TbSeckillOrder;
import com.dongdongshop.pojo.user.TbUser;
import com.dongdongshop.service.seckill.SeckillService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 秒杀
 */
@Controller
@RequestMapping("seckill")
public class SeckillController {

    @Reference(check = false)
    private SeckillService seckillService;

    Logger log = LoggerFactory.getLogger(SeckillController.class);

    /**
     * 跳转秒杀列表页 并携带所有状态良好的秒杀商品信息集合
     */
    @RequestMapping("tolist")
    public String tolist(Model model){

        List<TbSeckillGoods> seckillList =  seckillService.getSecKillGoods();
        model.addAttribute("seckillList",seckillList);
        return "seckill/seckill-index";
    }


    /**
     * 根据秒杀商品id查询秒杀商品信息并跳转秒杀商品详情页
     */
    @RequestMapping("findSeckillById/{id}")
    public String findSeckillById(@PathVariable("id")Long id,Model model){
        TbSeckillGoods seckillGoods = seckillService.findSeckillById(id);
        model.addAttribute("seckillGoods",seckillGoods);
        return "seckill/seckill-item";
    }

    /**
     * 秒杀提交订单
     */
    @RequestMapping("submitOrder")
    @ResponseBody
    public Result submitOrder(Long seckillId){
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        if(user == null){
            return new Result(501,"请先登录");
        }
        Result order = findOrder(seckillId);
        String data = (String)order.getData();
        if("ok".equals(data)){
            return new Result(450,"您已购买过该商品");
        }
        try {
            //调用dubbo，提交订单，减库存等操作
            seckillService.submitOrder(seckillId,user.getId());
            return Result.response(ResultEnum.SUCCESS).setData(seckillId);
        }catch (RuntimeException e){
            log.error(ResultEnum.ERROR.getMessage(),e);
            //捕获service抛出的异常
            return new Result(500,"商品已售罄");
        }catch (Exception e){
            log.error(ResultEnum.ERROR.getMessage(),e);
            return new Result(500,"服务器异常");
        }
    }

    @RequestMapping("findOrder")
    @ResponseBody
    public Result findOrder(Long seckillId){
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        TbSeckillOrder seckillOrder = seckillService.findOrder(seckillId,user.getId());
        if(seckillOrder!=null){
            return Result.response(ResultEnum.SUCCESS).setData("ok");
        }
        return Result.response(ResultEnum.FAIL).setData("nook");
    }
}